package com.lib.framework.manager

import android.util.Log
import com.lib.framework.BuildConfig


/**
 * 日志工具类
 * @author keanbin
 */
object LogManager {

    private const val TAG = "LogManager"

    /**
     * 调试开关
     */
    var debug = BuildConfig.VERSION_TYPE != "Release" //BuildConfig.DEBUG
        set(value) {
            Log.e(TAG, "LogManager.debug = $value")
            field = value
        }

    fun v(tag: String?, msg: String): Int {
        if (!debug) return 0

        return Log.v(tag, msg)
    }

    fun v(tag: String?, msg: String, tr: Throwable?): Int {
        if (!debug) return 0

        return Log.v(tag, msg, tr)
    }

    fun d(tag: String?, msg: String): Int {
        if (!debug) return 0

        return Log.d(tag, msg)
    }

    fun d(tag: String?, msg: String, tr: Throwable?): Int {
        if (!debug) return 0

        return Log.d(tag, msg, tr)
    }

    @JvmStatic
    fun i(tag: String?, msg: String): Int {
        if (!debug) return 0

        return Log.i(tag, msg)
    }

    fun i(tag: String?, msg: String, tr: Throwable?): Int {
        if (!debug) return 0

        return Log.i(tag, msg, tr)
    }

    fun w(tag: String?, msg: String): Int {
        return Log.w(tag, msg)
    }

    fun w(tag: String?, msg: String, tr: Throwable?): Int {
        return Log.w(tag, msg, tr)
    }

    fun w(tag: String?, tr: Throwable?): Int {
        return Log.w(tag, tr)
    }

    @JvmStatic
    fun e(tag: String?, msg: String): Int {
        return Log.e(tag, msg)
    }

    fun e(tag: String?, msg: String, tr: Throwable?): Int {
        return Log.e(tag, msg, tr)
    }

    @JvmStatic
    fun e(tag: String?, tr: Throwable?): Int {
        return Log.e(tag, "ERROR", tr)
    }

    /**
     * 分段打印出较长log文本
     * @param tag         打印log的标记
     * @param logContent  打印文本
     * @param showLength  规定每段显示的长度（AndroidStudio控制台打印log的最大信息量大小为4k）
     */
    fun showLargeLog(tag: String, logContent: String, showLength: Int = 3900) {
        if (logContent.length > showLength) {
            val show = logContent.substring(0, showLength)
            i(tag, show)
            /*剩余的字符串如果大于规定显示的长度，截取剩余字符串进行递归，否则打印结果*/
            if (logContent.length - showLength > showLength) {
                val partLog = logContent.substring(showLength, logContent.length)
                showLargeLog(tag, partLog, showLength)
            } else {
                val printLog = logContent.substring(showLength, logContent.length)
                i(tag, printLog)
            }

        } else {
            i(tag, logContent)
        }
    }
}